package com.atguigu.yygh.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.service.PaymentInfoService;
import com.atguigu.yygh.order.service.WxPayService;
import com.atguigu.yygh.order.utils.HttpRequestHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author luoyin
 * @Date 19:27 2022/10/10
 **/
@RestController
@RequestMapping("/user/wxPay")
public class WxPayController {

    @Autowired
    private WxPayService wxPayService;

    //生成支付二维码
    @GetMapping("/createNative/{orderId}")
    public R createNative(@PathVariable Long orderId) throws Exception {
        String url = wxPayService.createNative(orderId);
        return R.ok().data("url",url);
    }

    //查看支付状态
    @GetMapping("/queryPayStatus/{orderId}")
    public R queryPayStatus(@PathVariable Long orderId) throws Exception {
        Map<String,String> map = wxPayService.queryPayStatus(orderId);
        String tradeState = map.get("trade_state");
        if(tradeState == null){
            return R.error().message("查询出错");
        }
        if ("SUCCESS".equals(tradeState)){

            //修改订单状态,修改支付状态,修改第三方医院
            wxPayService.updatePayStatus(orderId,map);
            return R.ok();
        }
        return R.ok().message("支付中");
    }
}
